"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.broadcastMiddleware = void 0;
const Administrator_1 = require("../../lib/Administrator");
const log4js_1 = require("log4js");
const Classrooms_1 = require("../../lib/Classrooms");
const utils_1 = require("../../utils/utils");
const logger = (0, log4js_1.getLogger)('socket/broadcastMiddleware');
async function broadcastMiddleware(app, io) {
    const { socket } = app.locals;
    const { query } = socket.handshake;
    socket.join(Administrator_1.ALL_MEMBER_CLASSROOM);
    if (!socket.broadcastCreatedForAdmin) {
        socket.broadcastCreatedForAdmin = function classroomCreated(data) {
            socket
                .broadcast
                .to(Administrator_1.ADMINISTRATOR)
                .emit('classroom:created', data);
            if (socket.broadcastClassroomForAdmin) {
                socket.broadcastClassroomForAdmin();
            }
        };
    }
    if (!socket.broadcastClassDissolveForAdmin) {
        socket.broadcastClassDissolveForAdmin = function classroomDissolve(data) {
            socket
                .broadcast
                .to(Administrator_1.ADMINISTRATOR)
                .emit('classroom:dissolve', data);
        };
    }
    if (!socket.broadcastMemberLeaveForAdmin) {
        socket.broadcastMemberLeaveForAdmin = function memberLeave(data) {
            socket
                .broadcast
                .to(Administrator_1.ADMINISTRATOR)
                .emit('left', data);
        };
    }
    if (!socket.broadcastMemberForAdmin) {
        socket.broadcastMemberForAdmin = function memberState(data) {
            socket
                .broadcast
                .to(Administrator_1.ADMINISTRATOR)
                .emit('member:state', data);
        };
    }
    if (!socket.broadcastClassroomForAdmin) {
        socket.broadcastClassroomForAdmin = function allClassroom() {
            const data = Classrooms_1.Classrooms.toArray();
            socket
                .broadcast
                .to(Administrator_1.ADMINISTRATOR)
                .emit('classroom', data);
        };
    }
    if (!socket.broadcastClassStateForAdmin) {
        socket.broadcastClassStateForAdmin = function classroomState(data) {
            logger.info(`classroom:state =>`, (0, utils_1.stringify)(data));
            socket
                .broadcast
                .to(Administrator_1.ADMINISTRATOR)
                .emit('classroom:state', data);
        };
    }
    if (!socket.broadcastMemberOfflineForAdmin) {
        socket.broadcastMemberOfflineForAdmin = function memberOffline(data) {
            socket
                .broadcast
                .to(Administrator_1.ADMINISTRATOR)
                .emit('member:offline', data);
        };
    }
    if (!socket.broadcastOnlineToAll) {
        socket.broadcastOnlineToAll = function memberOnline() {
            socket
                .broadcast
                .to(Administrator_1.ALL_MEMBER_CLASSROOM)
                .emit('member:online', query);
            if (socket.broadcastClassroomForAdmin) {
                socket.broadcastClassroomForAdmin();
            }
        };
    }
    if (!socket.broadcastOfflineToAll) {
        socket.broadcastOfflineToAll = function memberOffline() {
            socket
                .broadcast
                .to(Administrator_1.ALL_MEMBER_CLASSROOM)
                .emit('member:offline', query);
        };
    }
}
exports.broadcastMiddleware = broadcastMiddleware;
